import React, { Component } from 'react'
import { hasToken } from 'utils/storage'
import { Route, Redirect } from 'react-router-dom'

export default class AuthRoute extends Component {
  render() {
    const { component: Components, ...rest } = this.props
    return (
      <Route
        {...rest}
        render={(props) => {
          if (hasToken()) {
            return <Components {...props}></Components>
          } else {
            return (
              <Redirect
                to={{
                  pathname: '/login',
                  state: { pathname: props.location.pathname },
                }}
              ></Redirect>
            )
          }
        }}
      ></Route>
    )
  }
}
